/*document.getElementById('i111').onclick = function(){
    if (!check()){
                alert("请核对确认后再提交!")
                return;
            }
            document.forms[0].submit();
            return;
}*/
//刷新or取消
function refresh() {
    location.reload();
}
function refresh1() {
    location.reload();
}
function sub(){
    if (!check()){
        alert("请核对确认后再提交!")
        return;
    }
    var aeskey = "",
        range = 16,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z'];
    for(var i=0; i<range; i++){
        pos = Math.round(Math.random() * (arr.length-1));
        aeskey += arr[pos];
    }

    //var pwd = document.getElementById("userpwd").value;
    var userpwd = document.tijiao.text2.value;
    var pwd = document.tijiao.text3.value
    if(userpwd == ""){
        alert("密码不能为空!");
    }else {
        var pwds = CryptoJS.enc.Utf8.parse(pwd)
        var _word = CryptoJS.enc.Utf8.parse(userpwd),
            _key = CryptoJS.enc.Utf8.parse(aeskey),
            _iv = CryptoJS.enc.Utf8.parse(aeskey);
        var encrypted1 = CryptoJS.AES.encrypt(_word, _key, {
            iv: _iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        var encrypted1 = CryptoJS.AES.encrypt(pwds, _key, {
            iv: _iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        //document.getElementById("userpwd").value = encrypted1.toString();
        document.tijiao.text2.value = encrypted1.toString();
        document.tijiao.text3.value = encrypted1.toString()
        var publickey;
        $.ajax({
            url: "/uc/pubkey",
            type: "post",
            async: false,
            success: function (data) {
                publickey = data;
            }
        });
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publickey);
        var encrypted = encrypt.encrypt(aeskey);
        document.getElementById("key").value = encrypted;

        document.forms[0].submit();
        return;
    }

}

//用户名验证
function checkname(){
    var count =0;
    var div = document.getElementById("div1");
    div.innerHTML = "";
    var name1 = document.tijiao.text1.value;
    if (name1 == "") {
        div.innerHTML = "用户名不能为空！";
        document.tijiao.text1.focus();
        return false;
    }
    if (name1.length < 4 || name1.length > 16) {
        div.innerHTML = "长度4-16个字符";
        document.tijiao.text1.select();
        return false;
    }
    var charname1 = name1.toLowerCase();

    for (var i = 0; i < name1.length; i++) {
        var charname = charname1.charAt(i);
        if (!(charname >= 0 && charname <= 9) && (!(charname >= 'a' && charname <= 'z')) && (charname != '_')) {
            div.innerHTML = "用户名包含非法字符";
            document.form1.text1.select();
            return false;
        }
    }
    $.ajax({
        url: "/uc/checkusername",
        data: {username: name1},
        type: "POST",
        //dataType: "json",
        async:false,
        success: function (data) {
            count = data;
        }
    });
    if(count>0){
        div.innerHTML = "用户已存在!";
        return false;
    }
    return true;
}

//密码验证
function checkpassword(){
    var div = document.getElementById("div2");
    div.innerHTML = "";
    var password = document.tijiao.text2.value;
    if (password == "") {
        div.innerHTML = "密码不能为空";
        // document.tijao.text2.focus();
        return false;
    }
    if (password.length < 4 || password.length > 16) {
        div.innerHTML = "密码长度为4-16位";
        // document.tijiao.text2.select();
        return false;
    }
    return true;
}

function checkrepassword(){
    var div = document.getElementById("div3");
    div.innerHTML = "";
    var password = document.tijiao.text2.value;
    var repass = document.tijiao.text3.value;
    if (repass == "") {
        div.innerHTML = "密码不能为空";
        // document.tijiao.text3.focus();
        return false;
    }
    if (password != repass) {
        div.innerHTML = "密码不一致";
        // document.tijiao.text3.select();
        return false;
    }
    return true;
}

//手机号验证
function checkphone(){
    var count2 = 0;
    var div = document.getElementById("div4");
    div.innerHTML = "";
    var phone = document.tijiao.text4.value;
    if (phone == "") {
        div.innerHTML = "手机号不能为空";
        // document.tijao.text2.focus();
        return false;
    }
    if (phone.length != 11) {
        div.innerHTML = "请输入正确的手机号";
        // document.tijiao.text2.select();
        return false;
    }
    $.ajax({
        url: "/uc/checkuserphone",
        data: {phone: phone},
        type: "POST",
        //dataType: "json",
        async:false,
        success: function (data) {
            //alert("data :" +data);
            count2 = data;
        }
    });
    if(count2>0){
        div.innerHTML = "该手机号已注册!";
        return false;
    }
    return true;
}

function check(){
    if (checkname() && checkpassword() && checkrepassword() && checkphone()) {
        return true;
    }
    else {
        return false;
    }
}